import Router from 'koa-router';
import axios from './utils/axios'
import Poi from '../dbs/models/poi'


let router = new Router({
    // 前缀
    prefix: '/search'
})

// 定义签名
const sign = '5be6f1e38999ac1af60cbaad320b60cc';


router.get('/top', async (ctx) => {

//     try {
//     let top = await Poi.find({
//       'name': new RegExp(ctx.query.input),
//       city: ctx.query.city
//     })
//     ctx.body = {
//       code: 0,
//       top: top.map(item => {
//         return {
//           name: item.name,
//           type: item.type
//         }
//       }),
//       type: top.length ? top[0].type : ''
//     }
//   } catch (e) {
//     ctx.body = {
//       code: -1,
//       top: []
//     }
//   }
    let {status, data: {top}} = await axios.get(`http://cp-tools.cn/search/top`, {
        params: {
            input: ctx.query.input || '',
            city: ctx.query.city,
            sign
        }
    })
    ctx.body = {
        top: status === 200 ? top : []
    }
})

// 热门城市city

router.get('/hotPlace' , async (ctx) => {
    // let city = ctx.store ? ctx.store.geo.position.city : ctx.query.city
    // try {
    //     let result = await Poi.find({
    //         city,
    //         type: ctx.query.type || '景点'
    //     }).limit(10)

    //     ctx.body = {
    //         code: 0,
    //         result: result.map(item => {
                    // return {
                    //     name: item.name,
                    //     type: item.type
                    // }
    //         })
    //     }
    // } catch (e) {
    //     ctx.body = {
    //         code: -1,
    //         result: []
    //     }
    // }

    let city = ctx.store ? ctx.store.geo.position.city : ctx.query.city
    let {status, data:{result}} = await axios.get(`http://cp-tools.cn/search/hotPlace`, {
        params: {
            city,
            sign
        }
    })
    ctx.body = {
        result: status === 200 ? result : []
    }
})



router.get('/resultsByKeywords', async (ctx) => {
    const {city, keyword} = ctx.query;
    let {
      status,
      data: {
        count,
        pois
      }
    } = await axios.get('http://cp-tools.cn/search/resultsByKeywords', {
      params: {
        city,
        keyword,
        sign
      }
    })
    ctx.body = {
      count: status === 200 ? count : 0,
      pois: status === 200
        ? pois
        : []
    }
  })


// 通过关键词获取内容
// router.get('/resultsByKeywords', async (ctx) => {
//     const {city, keyword} = ctx.query;
//     let {status,data:{count,pois}} = await axios.get('http://cp-tools.cn/search/resultsByKeywords', {
//         params: {
//             city,
//             keyword,
//             sign,
//         }
//     })
//     ctx.body = {
//         count: status === 200 ? count: 0,
//         pois: status === 200 ? pois : []
//     }
// })


router.get('/products',  async (ctx) => {
  let keyword = ctx.query.keyword || '旅游'
  let city = ctx.query.city || '上海'
  let {status, data: {product,more}} = await axios.get('http://cp-tools.cn/search/products', {
    params: {
      keyword,
      city,
      sign
    }
  })
  if (status === 200) {
    ctx.body = {
      product,
      more: ctx.isAuthenticated() ? more : [],
      login: ctx.isAuthenticated()
    }
  }
  else {
    ctx.body = {
      product: {},
      more: ctx.isAuthenticated() ? more: [],
      login: ctx.isAuthenticated()
    }
  }
})

export default router
